<?php
class e extends page {
	public $page_title = "Mit nézel?";
	public $layout = "episode";
	private $seriesname;
	private $episode; 
	
	public function load(){
		
		browserReqRedirect("/unsupported_browser");
		
		//$this->html->cache = false;
		$this->html->name = 'ep';
		$this->include_fb = true;
		$this->html->link('defaultpointytip', array('options' => array('id' => 'defaultPointyTipStyle')));
		
	
		$this->html->link(array('css-reset','style_new','review', 'fauxSelect'));
	
	
		$this->html->mootools(array(
			'Request.HTML',
			'Request.JSON',
			'Lang',
			'Form.Validator.Inline',
			'Assets',
			'Tips',
			'Tips.Pointy',
			'MooScroller',
			'Form.Validator.Tips',
			'Form.Validator.Hungarian',
			'Fx.Move',
			'Fx.Reveal',
			'Fx.Scroll',
			'Fx.Transitions',
			'Fx.Elements',
			'Fx.Tween',
			'Color',
			'Drag.Move',
			'Date.Extras',
			'Date.Hungarian',
			'Keyboard',
			'URI',
			'LazyLoad',
			'Cookie',
			'Form.Request'
		));
			
		$this->html->js(array(
			'review',
			'rank.class',
			'sprintf',
			'moocheck',
			'common'
		), array('compress' => true));
		
		//$this->html->js('excanvas_r43', array('conditional' => 'IE', 'compress' => false));
		$this->html->link('iefix', array('conditional' => 'IE'));	
				
		$this->seriesname = request_var('value1', false);
		
		$this->episode = request_var('value2',false);
		if(!$this->seriesname or !$this->episode)
			return $this->error->e404();
		$this->seriesname = urldecode($this->seriesname);
		
		$this->page_title .= " &bull; {$this->seriesname} {$this->episode}";
		
		$this->explodeEpisode();
		if(!$data = $this->getEpisodeByName())
			return $this->error->e404();
		$this->template->assign('episode', $data);
		
	}
	
	private function getEpisodeByName(){
		$result = false;
		$rnk = new Rank;
		
		$sql = "SELECT s.`seriesname`, e.`id`, e.`episodename`, e.`seasonnumber`, e.`episodenumber`, e.`overview`, e.`filename` FROM `" . SERIES_TABLE . "` s LEFT JOIN `" . EPISODES_TABLE . "` e on e.`seriesid` = s.`id` WHERE s.`seriesname` = '{$this->seriesname}' and e.`seasonnumber` = '{$this->episode['season']}' and e.`episodenumber` = '{$this->episode['episode']}' LIMIT 1";
		$this->db->query($sql);
		if($this->db->affected_rows() == 0)
			return false;
			
		list($result) = $this->db->fetch_assoc();
		$result['filename'] = (!empty($result['filename'])) ? md5($result['filename']) : null;
		$result['rank'] = $rnk->getRank($result['id']);
		$result['avrank'] = $rnk->getAvRank($result['id']);

		return $result;
	}
	
	private function explodeEpisode(){
		$eps = explode("x",$this->episode);
		$this->episode = array(
			"season" => (int)$eps[0],
			"episode" => (int)$eps[1]
		);
	}
	
}

?>